﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestException
{
    class Program
    {
        static void Main(string[] args)
        {
            int i=Test();
            //User user = TestUser();
            //Console.ReadLine();
        }

        static int Test()
        {
            int i;
            try
            {
                throw new Exception();
                //return i=1;
            }
            catch
            {
                throw;
            }
            finally
            {
                i = 2;
                Console.WriteLine("执行挖完毕");
             }
        }

        private static User TestUser()
        {
            var user = new User() {Name = "Mike", Id = 1};
            try
            {
                return user;
            }
            finally
            {
                user.Name = "Rose";
                user.Id = 2;
                user = null;
            }
        }
    }

    public class User
    {
        public string Name{ get; set; }
        public int Id{ get; set; }
    }

    public class ClassShouldDisposeBase : IDisposable
    {
        string _methodName;

        public ClassShouldDisposeBase(string methodName)
        {
            _methodName = methodName;
        }
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
            Console.WriteLine($"在方法：{_methodName}中被释放");
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                //执行基本的清理代码
            }
        }

        ~ClassShouldDisposeBase()
        {
            this.Dispose(false);
        }
    }
}
